# version 1.02 for cmake 2.6++
# IDAPRO plugins compilation cmake scripts common, the plugins should be in
# plugins dir or the same dir-level as default plugins dir
# dir should be orgnized as follows:
# /idapcomm
# /idasdk5x
#    /plugins
#    /myplugins/CMakeLists.txt include this script
# rev: 02 Unix systems test passed
# rev: 01 IDASDK 5.6 changed the lib directory place, so we changed accordingly
#


set(IDP_DEFS_COMM           "-D__IDP__" "-DUSE_DANGEROUS_FUNCTIONS" "-DUSE_STANDARD_FILE_FUNCTIONS")
set(IDP_DEFS_COMM64         "")
set(IDP_INCL_COMM           "../../include" "${CMAKE_CURRENT_LIST_DIR}/../idapcomm")

if( NOT WIN32 )
  option(BTOS_FREE_BSD    "set target os freebsd" OFF)
endif()

if( WIN32 OR CYGWIN)
  option(BTOS_UNDER_CE    "target os windows CE" OFF)
  #message("-> IS WIN32/CYGWIN/MINGW Platform")

  set(IDP_LINK_LIB        "ida.lib")
  set(IDP_LINK_LIB64      "ida.lib")
  set(IDP_SUFFIX32        ".plw")
  set(IDP_SUFFIX64        ".p64")
  
  if( CYGWIN )
    set(IDP_DEFS_COMM       -D__LINUX__ ${IDP_DEFS_COMM})
    set(IDP_DEFS_COMM64     __EA64__=1 ${IDP_DEFS_COMM64})
  else()
    set(IDP_DEFS_COMM       -D__NT__ ${IDP_DEFS_COMM})
    set(IDP_DEFS_COMM64     __EA64__=1 ${IDP_DEFS_COMM64})
  endif()

  if( MSVC ) 
    set(IDP_LIB_PATH    ${PROJECT_SOURCE_DIR}/../../lib/vc.w32)   #libvc.w32)
    set(IDP_LIB_PATH64  ${PROJECT_SOURCE_DIR}/../../lib/vc.w64)   #libvc.w64)

    if( CMAKE_CL_64 )
      # windows x64 might not work now
      message(-FATAL_ERROR "winx64 is not supported yet!")
    endif()
    
    # CE should be supported
    if( BTOS_UNDER_CE )
      set(IDP_DEFS_COMM       -DUNDER_CE ${IDP_DEFS_COMM})
    endif( BTOS_UNDER_CE )
  elseif( BORLAND )
    set(IDP_LIB_PATH    ${PROJECT_SOURCE_DIR}/../../lib/bor.w32)    #/libbor.w32) 
    set(IDP_LIB_PATH64  ${PROJECT_SOURCE_DIR}/../../lib/bor.w64)    #/libbor.w64) 
  else()
    # must be gcc on win32 -> most likely is MinGW
    message("-> IS BUILDING PROJECT UNDER MINGW/CYGWIN Platform")
  
    set(IDP_LINK_LIB    "ida.a")
    set(IDP_LINK_LIB64  "ida.a")
    set(IDP_LIB_PATH    ${PROJECT_SOURCE_DIR}/../../lib/gcc.w32) #/libgcc.w32)  
    set(IDP_LIB_PATH64  ${PROJECT_SOURCE_DIR}/../../lib/gcc.w64) #/libgcc.w64)  
  endif()

elseif( APPLE )
  set(IDP_SUFFIX32        ".pmc")
  set(IDP_SUFFIX64        ".pmc64")

  set(IDP_DEFS_COMM       -D__MAC__ ${IDP_DEFS_COMM})
  set(IDP_DEFS_COMM64     __EA64__=1 ${IDP_DEFS_COMM64})

  set(IDP_LINK_LIB        "libida.dylib")
  set(IDP_LINK_LIB64      "libida64.dylib")
  set(IDP_LIB_PATH        ${PROJECT_SOURCE_DIR}/../../lib/gcc32.mac)   #/libgcc32.mac)  
  set(IDP_LIB_PATH64      ${PROJECT_SOURCE_DIR}/../../lib/gcc64.mac64)   #/libgcc64.mac)  

  get_filename_component(__MYMODESDIR ${CMAKE_CURRENT_LIST_FILE} PATH)
  include("${__MYMODESDIR}/cmkutils.cmake")
  
  # on macos 10.6 x32, gcc 4.2.1 default to generate 64 arch target
  util_add_cflags("-arch i386")

elseif( UNIX )
 
  if( BTOS_FREE_BSD ) 
    set(IDP_SUFFIX32      ".pbsd")
    set(IDP_SUFFIX64      ".pbsd64")
    set(IDP_DEFS_COMM     -D__BSD__ ${IDP_DEFS_COMM})
  else()
    set(IDP_SUFFIX32      ".plx")
    set(IDP_SUFFIX64      ".plx64")
    set(IDP_DEFS_COMM     -D__LINUX__ ${IDP_DEFS_COMM})
  endif()
    
  set(IDP_DEFS_COMM64     __EA64__=1 ${IDP_DEFS_COMM64})

  set(IDP_LINK_LIB        "libida.so")
  set(IDP_LINK_LIB64      "libida64.so")
  set(IDP_LIB_PATH        ${PROJECT_SOURCE_DIR}/../../lib/gcc32.lnx) #/libgcc32.lnx)   
  set(IDP_LIB_PATH64      ${PROJECT_SOURCE_DIR}/../../lib/gcc64.lnx64) #/libgcc64.lnx)   
else()
  message(-FATAL_ERROR    "the target is not supported yet!")
endif()

include("${CMAKE_CURRENT_LIST_DIR}/idabpath.cmake")
if(IDA_BINPATH)
  set(CMAKE_INSTALL_PREFIX ${IDA_BINPATH})
endif()

if(WIN32)
  set(IDP_LINK_LIB_FULLPATH   "${IDP_LIB_PATH}/${IDP_LINK_LIB}")
  set(IDP_LINK_LIB_FULLPATH64 "${IDP_LIB_PATH64}/${IDP_LINK_LIB64}")
else()
  link_directories(${IDP_LIB_PATH} ${IDP_LIB_PATH64})
  set(IDP_LINK_LIB_FULLPATH   "${IDP_LINK_LIB}")
  set(IDP_LINK_LIB_FULLPATH64 "${IDP_LINK_LIB64}")

  set(LINK_SHARED_LIBC TRUE)
endif()


# make the default link static runtime libs
# this is only needed under windows, Unix glibcs may not work properly
# if link static glibc and c++ exception is used the .sos may not work
include("${CMAKE_CURRENT_LIST_DIR}/lnkslibc.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/vs10port.cmake")
